// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
   // 取出主分类
   // 取出次分类
   // 取出分类下 选项
	async getdiagnosisAction(){
		if (this.isGet) {
			let cates = await this.model('or_diagnosiscategory').where({orcode:1,pid:1}).select();
			return this.json(cates);
		}
		let orcode = await this.model('or_department').where({id:this.userinfo.departmentid}).find();
		let mains = await this.model('or_diagnosiscategory').where({orcode:1,pid:1}).select();
		let minors = await this.model('or_diagnosiscategory').where({orcode: orcode.orcode}).select();
		let options = await this.model('or_diagnosis').where({orcode: orcode.orcode}).select();
		let data = {
			mains: mains,
			minor: minors,
			opts: options,
		}
		return this.json(data);
	}

	// 取出对应类型下的各个诊断选项
	/*async getdiagnosisoptionAction(){
		let orcode = await this.model('or_department').where({id:this.userinfo.departmentid}).find();
		if (this.isPost) {
			let id = this.post('id');
			let res = await this.model('or_diagnosis').where({categoryid:id}).select();
			return this.json(res);
		}else{
			let categoryid = this.get('categoryid');
			let categoryoptionarr = [];
			categoryoptionarr = categoryid.split(',');
			let res = await this.model('or_diagnosis').where({orcode:orcode.orcode,categoryid:['IN',categoryoptionarr]}).select();
			return this.json(res);
		}
	}*/

	// 增加和修改诊断类型
	async adddiagnoseAction(){
		let orcode = await this.model('or_department').where({id:this.userinfo.departmentid}).find();
		let data = JSON.parse(this.post('data'));
		// 判断诊断类型名称是否重复
		let isrepeat = await this.model('or_diagnosiscategory').where({orcode: orcode.orcode, name: data.name}).find();
		/*if (!think.isEmpty(isrepeat)) {
			return this.fail('诊断类型名称重复！');
		}*/

		if (data.id) {
			let upd = await this.model('or_diagnosiscategory').where({id: data.id}).update(data);
			return upd?this.success('编辑成功'):this.fail('编辑失败');
		}
		if (data.pid) {
			let adddata = {
				name: data.name,
				orcode: orcode.orcode,
				pid: data.pid
			}
			let res = await this.model('or_diagnosiscategory').add(adddata);
			return res?this.success('添加成功'):this.fail('添加失败');
		}

	}

	// 增加和修改诊断选项
	async addoptionAction(){
		let orcode = await this.model('or_department').where({id:this.userinfo.departmentid}).find();
		let data = JSON.parse(this.post('data'));
		let isrepeat = await this.model('or_diagnosis').where({orcode: orcode.orcode, name: data.name}).find();
		if (!think.isEmpty(isrepeat)) {
			return this.fail('诊断选项名称重复！');
		}
		if (data.id) {
			let addone = {
				categoryid: data.cid,
				name: data.name,
				orcode: orcode.orcode
			}
			let upd = await this.model('or_diagnosis').where({id: data.id}).update(addone);
			return upd?this.success('编辑成功'):this.fail('编辑失败');
		}
		let adddata = {
			name: data.name,
			orcode: orcode.orcode,
			categoryid: data.cid,
		}
		let res = await this.model('or_diagnosis').add(adddata);
		return res?this.success('添加成功'):this.fail('添加失败');
	}

	// 删除的诊断选项
	async deleteoptionAction(){
		let id = this.post('id');
		let model = this.model('or_diagnosis');
		await model.transaction(async () => {
			try {
				let isempty = await this.model('or_shop_guest_zengduan').db(model.db()).where({categoryid: id}).getField('id');
				if (!think.isEmpty(isempty)) {
					let delone = await this.model('or_shop_guest_zengduan').db(model.db()).where({id: ['IN'], isempty}).delete();
					if (!delone) {
						await model.rollback();
						return this.fail('删除失败');
					}
				}

				let deltwo = await this.model('or_diagnosis').db(model.db()).where({id: id}).delete();
				if (deltwo) {
					await model.commit();
					return this.success('删除成功');
				} else {
					await model.rollback();
					return this.fail('删除失败');
				}
			} catch(e) {
				console.log(e);
				await model.rollback();
				return this.fail('删除失败');
			}
		})
	}

	// 删除诊断类型及类型下对应的选项
	async deletecategoryAction(){
		let id = this.post('id');
		let model = this.model('or_diagnosiscategory');
		await model.transaction(async () => {
			try {
				let isempty = await this.model('or_diagnosis').db(model.db()).where({categoryid: id}).getField('id');
				if (!think.isEmpty(isempty)) {

					let isone = await this.model('or_shop_guest_zengduan').db(model.db()).where({categoryid: ['IN', isempty]}).getField('id');

					if (!think.isEmpty(isone)) {
						let delzd = await this.model('or_shop_guest_zengduan').db(model.db()).where({id: ['IN', isone]}).delete();
						if (!delzd) {
							await model.rollback();
							return this.fail('删除类型失败');
						}
					}

					let reschilder = await this.model('or_diagnosis').db(model.db()).where({id: ['IN', isempty]}).delete();

					if (!reschilder) {
						await model.rollback();
						return this.fail('删除类型失败');
					}
				}
				let resparent = await model.where({id: id}).delete();
				if (resparent) {
					await model.commit();
					return this.success('删除类型成功');
				} else {
					await model.rollback();
					return this.fail('删除类型失败');
				}
			} catch(e) {
				await model.rollback();
				return this.fail('删除类型失败');
			}
		})
	}

	// 获取用户默认选项
	async getuseroptionAction(){
		if (this.isGet) {
			let data = this.get('uid');
			let res = await this.model('or_userdiagnosis').where({uid:data}).select();
			return this.json(res);
		}
	}




}